<?php
    require_once("./classes/sessao.php");
    require_once("./classes/interfaces.php");
    require_once("./classes/conexao.php");

    session_start();

    if (!isset($_SESSION['ANAWEB']))
    {
        $pagina = new Interfaces();
        $pagina->Header(Nao_Logado);
        $pagina->Body();
        echo Nao_Logado_Msg;
        $pagina->Footer();
    }
    else
    {
        $pagina = new Interfaces();
        $pagina->Header(" ANAWEB ");
        $pagina->Body();
        if (isset($_GET['file']) && isset($_POST['cat']))
        {
            $arq = base64_decode($_GET['file']);
            /*if ($arq == "./bibliotecas/maple_valid.xml")
            {*/
                require_once("./classes/casxml.php");
                $xml = new CasXML();
                $xml->preserveWhiteSpace=false;
                $xml->load($arq);
                $xml->formatOutput=true;

                $name = $_POST['nome'];
                $folder = $xml->getElementsByTagname("subject")->item($_POST['cat']);
                $description = $_POST['desc'];
                $sentence = $_POST['comandos'];
                $remarksentence = $_POST['comentarioscom'];
                $remarkexample = $_POST['comentarioscom'];
                $relatedcommand = $_POST['relcom'];
                $template = $_POST['templ'];
                $remarktemplate = $_POST['comentarios'];
                $exercisedescription = $_POST['exercicio'];
                $hint = $_POST['dica'];
                $solution = $_POST['solucao'];
                /*if (@$_FILES['ajuda']['size'] > 0)
                {
                    $uploaddir = "./bibliotecas/cas_filehelp/";
                    $userfile = basename($_FILES['ajuda']['name']);
                    $uploadfile = $uploaddir . $userfile;

                    if (move_uploaded_file($_FILES['ajuda']['tmp_name'], $uploadfile)) {
                        echo "File is valid, and was successfully uploaded.\n";
                    } else {
                        echo "Possible file upload attack!\n";
                    }
                    $filehelp = $userfile;
                } else
                {
                    $filehelp = '';
                }*/
                $filehelp = @$_POST['ajuda'];
                $xml->addItem( $name, $folder, $description,
                            $sentence, $remarksentence, $remarkexample,
                            $relatedcommand,$template,$remarktemplate,
                            $exercisedescription,$hint,$solution,$filehelp, @$_POST['topico'], @$_POST['subtopico'] );
                $xml->save($arq);
?>
            <?php echo Arvore_Alterada_Sucesso; ?>
            <br>
            <a href="index.php"><?php echo Voltar; ?></a>
<?php
            /*}
            elseif ($arq != "./bibliotecas/maple_valid.xml")
            {
                $xml = new DomDocument();
                $xml->preserveWhiteSpace=false;
                $xml->load($arq);
                $xml->formatOutput=true;
                $cats = $xml->getelementsbytagname("subject");
                if ($cats->length > $_POST['cat'])
                {
                    $item = $xml->createElement("item");
                    $item->setAttribute("name",$_POST['nome']);
                    $item = $cats->item($_POST['cat'])->appendChild($item);

                    $commands = $item->appendChild( $xml->createElement("commands") );
                        $command = $commands->appendChild($xml->createElement("command"));

                    $tryyourself = $item->appendChild($xml->createElement("try_yourself"));
                        $try = $tryyourself->appendChild($xml->createElement("try"));

                    $teacher = $item->appendChild($xml->createElement("teacher"));
                        $ask = $teacher->appendChild($xml->createElement("ask"));

                    $examples = $item->appendChild($xml->createElement("examples"));

                    $comandos = explode("\r\n",$_POST['comandos']);
                    foreach ($comandos as $c)
                    {
                        $example = $examples->appendChild($xml->createElement("example",$c));
                    }

                    $filehelp = $item->appendChild($xml->createElement("filehelp"));

                    $description = $item->appendChild($xml->createElement("description",utf8_encode($_POST['desc'])));

                    $xml->save($arq);*/

                //}
            //}
        }
        $pagina->Footer();
    }
?> 

